//
//  TravelNotesCollectionViewController.m
//  TravelFirst
//
//  Created by lanou on 15/9/18.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "TravelNotesCollectionViewController.h"
#import "NotesCollectionViewCell.h"
#import "NotesModel.h"
#import "NotesDetailViewController.h"

@interface TravelNotesCollectionViewController () <UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TravelNotesCollectionViewController


#pragma mark - 加载数据 -

- (void)reloadView
{
    [LORequestManger GET:kNotesRecommend success:^(id response) {
        
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *logsDic in dic[@"logs"]) {
            NotesModel *notesModel = [[NotesModel alloc] init];
            [notesModel shareJsonWithDictionary:logsDic];
            notesModel.log_id = logsDic[@"id"];
            [notesModel shareJsonWithDictionary:logsDic[@"created_by"]];
            notesModel.home_fav_count = logsDic[@"fav_count"];
            [self.dataArray addObject:notesModel];
        }
        //NSLog(@"%@", self.dataArray);
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

static NSString * const reuseIdentifier = @"notesCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadView];
    
    self.collectionView.backgroundColor = kColor;
    
    [self.collectionView registerClass:[NotesCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    //NSLog(@"%@", NSStringFromCGRect(self.collectionView.frame));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.dataArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NotesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NotesModel *notesModel = self.dataArray[indexPath.row];
    [cell setValueWithModel:notesModel target:self action:@selector(nicknameButton:) tag:indexPath.row];
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidth - 30, kHeight / 2);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 8;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NotesDetailViewController *notesDetailVC = [[NotesDetailViewController alloc] init];
    notesDetailVC.log_id = [self.dataArray[indexPath.row] log_id];
    
    [self.navigationController pushViewController:notesDetailVC animated:YES];
    
}

- (void)nicknameButton:(UIButton *)button
{
    NSLog(@"个人信息");
}


#pragma mark - 懒加载 -

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
